<?php
include("interface.class.php");
include("table.class.php");
include("database.class.php");
include("accordion.class.php");

$inf = new webinterface();
$inf->set_title("Proxy List");

$db = new database();
$link = $db->connect();

/* Actions */
if (array_key_exists('action', $_GET) && array_key_exists('idproxy', $_GET) && is_numeric($_GET['idproxy'])) {
	switch ($_GET['action']) {
		case 'delete':
			$sql = "DELETE FROM proxys WHERE idproxy='" . $_GET['idproxy'] . "'";
			$res = mysql_query($sql, $link);
			break;
		case 'enable':
			$sql = "UPDATE proxys SET active=1 WHERE idproxy='" . $_GET['idproxy'] . "'";
			$res = mysql_query($sql, $link);
			break;
		case 'disable':
			$sql = "UPDATE proxys SET active=0 WHERE idproxy='" . $_GET['idproxy'] . "'";
			$res = mysql_query($sql, $link);
			break;
		case 'edit':
			$sql = "SELECT INET_NTOA(proxys.ip) AS ip, properties.property, proxy_property.value FROM proxys INNER JOIN proxy_property ON proxys.idproxy=proxy_property.idc_proxy INNER JOIN properties ON proxy_property.idc_property=properties.idproperty WHERE proxys.idproxy='" . $_GET['idproxy'] . "'";
			$res = mysql_query($sql, $link);
			$acc = new accordion();
			$acc->t_id = "proxy-property";	
			while ($row = mysql_fetch_array($res)){
				$section = split('-', $row['property'], 2);
				$section = $section[0];
				$acc->add_content($section,  $row['property'] . "<INPUT TYPE='text' name='" . $row['property'] . "' value='" . $row['value'] . "' /><br/>");
			}	
			$inf->add_docready_script($acc->script());
			$inf->add_content($acc->html());
			break;
	}
} else {

	$tab = new table();
	$tab->t_id="proxys";

	if (array_key_exists('active', $_GET) and $_GET['active']=="1"){
		$sql = "SELECT idproxy, active, INET_NTOA(ip) as ip, port, checked FROM proxys WHERE active=1";
		$only_active = True;
		$tab->set_head(array('IP', 'Port', 'Checked', 'Actions'));
	} else {
		$sql = "SELECT idproxy, active, INET_NTOA(ip) as ip, port, checked FROM proxys";
		$only_active = False;
		$tab->set_head(array('Active', 'IP', 'Port', 'Checked', 'Actions'));
	}

	$res = mysql_query($sql, $link);
	while ($row = mysql_fetch_array($res)) {

		$r_active	= $row['active'];
		$r_actions = "";
		$r_actions	.= '<a href="show_proxys.php?action=edit&idproxy=' . $row['idproxy'] . '"><img src="images/edit.png" /></a>';
		if ($r_active == 1){
			$r_active = "Yes";
			# Disable action
			$r_actions	.= '<a href="show_proxys.php?action=disable&idproxy=' . $row['idproxy'] . '"><img src="images/cross.png" /></a>';
		} else {
			$r_active = "No";
			# Enable action
			$r_actions	.= '<a href="show_proxys.php?action=enable&idproxy=' . $row['idproxy'] . '"><img src="images/accept.png" /></a>';
		}	
		$r_ip		= $row['ip'];	
		$r_port		= $row['port'];
		$r_checked	= $row['checked'];

		# Delete action
		$r_actions	.= '<a href="show_proxys.php?action=delete&idproxy=' . $row['idproxy'] . '"><img src="images/trash.png" /></a>';
		if ($only_active){
			$table_row = array($r_ip, $r_port, $r_checked, $r_actions);	
		} else {
			$table_row = array($r_active, $r_ip, $r_port, $r_checked, $r_actions);	
		}

		$tab->add_row($table_row);

	}
	$inf->add_docready_script($tab->script());
	$inf->add_content($tab->html());
} //End no action selected
mysql_close($link);
$inf->commit();
?>
